﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ExtremeSwank.Authentication.OpenID
{
    /// <summary>
    /// Functions that deal with quirks specific to certain OpenID Providers
    /// </summary>
    public static class Quirks
    {
        /// <summary>
        /// Check the discovered OpenID identity URL and confirm that it is compatible 
        /// with the provider we are authenticating against.
        /// </summary>
        /// <param name="url">OpenID identity URL</param>
        /// <returns>A corrected OpenID identity URL</returns>
        public static string CheckOpenIDUrl(string url)
        {
            // Fix AOL, ClaimID, and myLID URLs by removing trailing slash, if present.
            if (url.ToLower().Contains("://openid.aol.com/")
                || url.ToLower().Contains("://openid.claimid.com/")
                || url.ToLower().Contains("://mylid.net/"))
            {
                if (url.EndsWith("/"))
                {
                    return url.Trim('/');
                }
            }
            // For all others, ensure the URL identifier ends in a forward slash
            else
            {
                if (!url.EndsWith("/"))
                {
                    return url + "/";
                }
            }
            return url;
        }
        /// <summary>
        /// Check the OpenID identity URL and confirm whether or not the authentication
        /// mode should be changed due to quirks in the OpenID Provider's implementation.
        /// </summary>
        /// <param name="url">OpenID identity URL</param>
        /// <param name="mode">Mode currently set</param>
        /// <returns>Correct AuthenticationMode for the provider</returns>
        public static AuthenticationMode CheckOpenIDMode(string url, AuthenticationMode mode)
        {
            if (url.ToLower().Contains("://getopenid.com/"))
            {
                return AuthenticationMode.Stateless;
            }
            return mode;
        }
    }
}
